package com.ag.zrt.service.impl;

import java.util.Date;
import java.util.List;

import com.ag.common.AgException;
import com.ag.common.IDUtil;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.ag.bean.TbEvent;
import com.ag.bean.TbLogPlanStep;
import com.ag.common.IIDService;
import com.ag.processing.ProcessingPlans;
import com.ag.zrt.dao.TbCurrentEventDAO;
import com.ag.zrt.dao.TbLogPlanStepDAO;
import com.ag.zrt.service.ILogPlanStepService;

@Service("LogPlanStepService")
public class LogPlanStepServiceImpl implements ILogPlanStepService {

	@Resource(name = "ZRTTbLogPlanStepDAO")
	private TbLogPlanStepDAO logPlanStepDAO;
	@Resource(name = "ZRTTbCurrentEventDAO")
	private TbCurrentEventDAO currentEventDAO;
	@Resource(name = "iDServiceImpl")
	private IIDService idService;
	private String newId; // 增加步骤后返回新步骤ID

	public void setCurrentEventDAO(TbCurrentEventDAO currentEventDAO) {
		this.currentEventDAO = currentEventDAO;
	}

	public void setIdService(IIDService idService) {
		this.idService = idService;
	}

	public void setLogPlanStepDAO(TbLogPlanStepDAO logPlanStepDAO) {
		this.logPlanStepDAO = logPlanStepDAO;
	}

	@Override
	public List<TbLogPlanStep> findAll(String eventId) {
		// TODO Auto-generated method stub
		return logPlanStepDAO.findByProperty("tbEvent.EId", eventId);
	}

	@Override
	public String save(String eventId, String curStepId, String stepName) {
		// TODO Auto-generated method stub
		try {
			System.out.println("eventId------------------" + eventId
					+ "-----------------curStepId---------" + curStepId
					+ "----stepName-----------" + stepName);
			StringBuilder sb = new StringBuilder();
			sb.append("from TbLogPlanStep as lps where lps.tbEvent.EId = "
					+ eventId + " and lps.psId = '" + curStepId + "'");
			System.out.println(sb.toString()
					+ "//////////find curStep//////////////////");
			// 当前步骤
			TbLogPlanStep lps = logPlanStepDAO.find(sb.toString());
			// 新增步骤
			TbLogPlanStep newStep = fillValues(lps, stepName);

			logPlanStepDAO.save(newStep);

			System.out.println("newStepID-----------------------"
					+ newStep.getPsId());
			// 临时保存下一步骤的ID
			TbLogPlanStep nStepTemp = new TbLogPlanStep();
			// 临时保存上一步骤的ID
			TbLogPlanStep fStepTemp = new TbLogPlanStep();

			newStep.setTbLogPlanStep(null);

			if (lps.getTbLogPlanStep() != null) {
				// 用一临时变量保存前一环节指向下一环节的ID
				String IdTemp = lps.getTbLogPlanStep().getPsId();
				nStepTemp.setPsId(IdTemp);
				newStep.setTbLogPlanStep(nStepTemp);

				System.out.println("newStepNextID-----------------------"
						+ newStep.getTbLogPlanStep().getPsId());
			}

			fStepTemp.setPsId(newStep.getPsId());

			lps.setTbLogPlanStep(fStepTemp);

			System.out.println("oldStepNextID-----------------------"
					+ lps.getTbLogPlanStep().getPsId());

			logPlanStepDAO.attachDirty(newStep);
			logPlanStepDAO.attachDirty(lps);

			return newStep.getPsId();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;

	}

	public void test(String eventId, String curStepId, String stepName) {

	}

	@Override
	public TbLogPlanStep findById(String id) {
		// TODO Auto-generated method stub
		return logPlanStepDAO.findById(id);
	}

	@Override
	public String getNewID() {
		// TODO Auto-generated method stub
		return newId;
	}

	@Override
	public int getCurStepNum(String eventId) {
		// TODO Auto-generated method stub
		return currentEventDAO.findById(eventId).getCeNumber().intValue();
	}

	@Override
	public void createStartTime(String curStepId) {
		// TODO Auto-generated method stub
		System.out
				.println(curStepId
						+ "///////////////////////////curStepId/////////////////////////////");
		try {
			TbLogPlanStep lps = logPlanStepDAO.findById(curStepId);
			if (lps.getStartTime() == null) {
				lps.setStartTime(new Date());
				logPlanStepDAO.attachDirty(lps);
				System.out
						.println(lps.getStartTime()
								+ "***************createStartTime***********************");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	@Override
	public void createEndTime(String curStepId) {
		// TODO Auto-generated method stub
		try {
			System.out
					.println(curStepId
							+ "///////////////////////////curStepId/////////////////////////////");
			TbLogPlanStep lps = logPlanStepDAO.findById(curStepId);
			if (lps.getEndTime() == null) {
				lps.setEndTime(new Date());
				logPlanStepDAO.attachDirty(lps);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	/*
	 * 填新环节的相关信息
	 */
	public TbLogPlanStep fillValues(TbLogPlanStep lps, String newName) {

		try {
			TbLogPlanStep newStep = new TbLogPlanStep();

			newStep.setPsName(newName);

			newStep.setTbPlan(lps.getTbPlan());

			TbEvent e = new TbEvent();

			e.setEId(lps.getTbEvent().getEId());

			newStep.setTbEvent(e);

			String newId = idService.findTablePK(IDUtil.TB_LOG_PLAN_STEP);

			newStep.setPsId(newId);

			return newStep;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
